package com.coordinate.servlets;




import com.coordinate.db.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Iterator;

import javax.jdo.JDOException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.*;

import org.apache.commons.io.IOUtils;

import com.google.appengine.api.labs.taskqueue.Queue;
import com.google.appengine.api.labs.taskqueue.QueueFactory;
import com.google.appengine.api.labs.taskqueue.TaskOptions.Method;
import static com.google.appengine.api.labs.taskqueue.TaskOptions.Builder.*;

import com.coordinate.services.*;
import com.coordinate.core.factory.AppStackFactory;
import com.coordinate.core.kernel.serviceprovider.ServiceProvider;
import com.coordinate.core.kernel.serviceprovider.impl.CEmailServiceProvider;
import com.coordinate.core.message.*;
import com.coordinate.core.message.impl.*;
import com.coordinate.core.type.LangText;
import com.coordinate.core.type.impl.CLangText;
import com.devicemantra.coordinate.ds.tests.PMF;
import com.devicemantra.coordinate.tasks.CTask;
import com.devicemantra.coordinate.tasks.CTaskFactory;
import com.google.appengine.api.datastore.Text;


@SuppressWarnings("serial")
public class TextMessageServiceServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		
		System.out.println(req.getParameter("anglicia"));
		TextMessage lTm = new CTextMessage();
		lTm.add("en",new Text(req.getParameter("anglicia")));
		lTm.add("ar",new Text(req.getParameter("arabia")));
		lTm.setSubject(req.getParameter("subject"));
		
		LangText lTxt = new CLangText("Simithaaa",new Text("My own language"));
		
		PersistenceManager lPm = PMF.get().getPersistenceManager();
		 
		try{
				lPm.makePersistent(lTm);   
				lPm.makePersistent(lTxt);
		}
		catch(JDOException e)
		{ 
		  e.printStackTrace();	
		  System.out.println(e.getMessage());
		
		}
		
		try{
			Query query = lPm.newQuery(CTextMessage.class);
			Collection lC = (Collection) query.execute();
			
			Iterator lIter = lC.iterator();
			
			while(lIter.hasNext())
			{
				CTextMessage lMsg = (CTextMessage) lIter.next();
				System.out.println("Size of Msg List is :"+ lMsg.getMsgList().size());
				int i = 0;
				while(i < lMsg.getMsgList().size())
				{
					CLangText lTxt2 = (CLangText)lMsg.getMsgList().get(i);
					System.out.println(lTxt2.toString());
					i++;
				}
			}
			
			System.out.println("The size is :"+ lC.size());

		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally{
			lPm.close();
		}
		
		ServiceProvider lSp = AppStackFactory.getStack().getServiceProviderFactory().getServiceProvider("email");
		lSp.send(lTm);
		
		lSp = AppStackFactory.getStack().getServiceProviderFactory().getServiceProvider("sms");
		lSp.send(lTm);
		
		lSp = AppStackFactory.getStack().getServiceProviderFactory().getServiceProvider("text2speech");
		lSp.send(lTm);
		
		resp.setContentType("text/plain");
		resp.getWriter().println("Message Sent");
		resp.getWriter().close();
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException {
		doPost(req,resp);
	}

}
